package org.sdkj.device.test.tcp.utils;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.Arrays;
import java.io.InputStream;
import java.io.OutputStream;

/**处理网络数据
 * @author lenovo
 *
 */
public class DevConn extends Thread {
	public Socket socket = null;
	InputStream in_s = null;
	OutputStream out_s = null;
	int timeout = 0;
	public static int RECV_BUF_MAX = 64 * 1024;

	/**
	 * 计算校验和
	 * 
	 * @param buf
	 * @param len
	 * @return
	 */
	public static int CheckSum(byte[] buf, int len) {
		int Sum = 0;
		int i;

		for (i = 0; i < len; i++) {
			Sum += buf[i] & 0xff;
		}
		return Sum & 0xffff;
	}

	/**
	 * 断开连接
	 */
	private void ExitDevConn() {
		try {
			in_s.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			out_s.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 初始化socket参数
	 */
	private void initSocket() {
		try {
			socket.setTcpNoDelay(false);
		} catch (SocketException e4) {
			// TODO Auto-generated catch block
			e4.printStackTrace();
		}
		try {
			socket.setSoTimeout(3000); // 设置接收超时为3秒
		} catch (SocketException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		try {
			socket.setReceiveBufferSize(RECV_BUF_MAX);
		} catch (SocketException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		System.out.println("new dev run...");
		try {
			in_s = (InputStream) socket.getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			out_s = (OutputStream) socket.getOutputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	
	boolean first=true;
	
	public void run() {
		int RecvByte = 0;
		byte[] RecvBuf = new byte[RECV_BUF_MAX];
		initSocket();
		for (;;) {
			try {
				RecvByte = in_s.read(RecvBuf);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				// e1.printStackTrace();
				if (timeout++ > 22)
					break; // 如果超时则退出
				continue;
			}
			if (RecvByte <= 0)
				continue;

			timeout = 0;
			System.out.println("RecvByte = " + RecvByte);
			if (RecvBuf[0] == (byte) 0xDD && RecvBuf[1] == (byte) 0xCC) {
				int address = RecvBuf[2] & 0xff;
				int cmd = RecvBuf[3] & 0xff;
				int datalen = (RecvBuf[4] & 0xff) + ((RecvBuf[5] & 0xff) << 8);
				System.out.println("接收到新包:address = " + address + ",cmd = " + cmd + ",datalen = " + datalen);
				if (RecvByte < (datalen + 8)) {
					int remain = (datalen + 8) - RecvByte;
					// 接收剩余数据
					for (;;) {
						// SendACKToClient();
						try {
							RecvByte += in_s.read(RecvBuf, RecvByte, remain);
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							System.out.println("接收超时退出连接");
							ExitDevConn();
							return;
						}
						remain = (datalen + 8) - RecvByte;
						System.out.println("RecvByte " + RecvByte + " remain " + remain);
						if (RecvByte >= (datalen + 8)) {
							System.out.println("接收完成");
							break;
						}
					}
				}
				int checksum1 = CheckSum(RecvBuf, datalen + 6);
				int checksum2 = (RecvBuf[datalen + 6] & 0xff) + ((RecvBuf[datalen + 7] & 0xff) << 8);
				if (checksum1 != checksum2) {
					System.out.println("校验和错误 CHECKSUM1 = " + checksum1 + "CHECKSUM2 = " + checksum2);
				} else {
					//System.out.println("-------------处理数据包------Start------------");
					MessageHandle handle = MessageHandle.getInstance(in_s, out_s, socket);
					
					
					handle.handle(cmd,Arrays.copyOfRange(RecvBuf, 6, 6 + datalen),datalen);
					//System.out.println("-------------处理数据包------end--------------");
				}
			} else {
				System.out.println("包头错误");
			}
		}
		ExitDevConn();
	}
}
